package com.ihighest.app.infox.im;

import io.rong.common.ParcelUtils;
import io.rong.imlib.MessageTag;
import io.rong.imlib.model.MessageContent;

import java.io.UnsupportedEncodingException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Parcel;
import android.util.Log;

/**
 * 自定义问题消息
 * 
 * 
 * @author Vity Created at 2015-11-23 上午11:29:37
 * @version 1.0
 */
@MessageTag(value = "Infox:IssueMsg", flag = MessageTag.NONE)
public class IssueMessage extends MessageContent {

	private String text;

	private String area;

	private String crop;

	private String weather;

	private String manure;

	private String gold;

	private String voiceUri;

	private String[] imageUrls;

	private JSONObject jsonObj;

	public static IssueMessage obtain(JSONObject jsonObject) {

		try {
			return new IssueMessage(jsonObject.toString().getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}

	public IssueMessage(byte[] data) {
		String jsonStr = null;

		try {
			jsonStr = new String(data, "UTF-8");
		} catch (UnsupportedEncodingException e1) {

		}

		try {
			jsonObj = new JSONObject(jsonStr);
			if (jsonObj.has("text")) {
				text = jsonObj.optString("text");
			}
			if (jsonObj.has("area")) {
				area = jsonObj.optString("area");
			}
			if (jsonObj.has("crop")) {
				crop = jsonObj.optString("crop");
			}
			if (jsonObj.has("weather")) {
				weather = jsonObj.optString("weather");
			}
			if (jsonObj.has("manure")) {
				manure = jsonObj.optString("manure");
			}
			if (jsonObj.has("gold")) {
				gold = jsonObj.optString("gold");
			}
			if (jsonObj.has("voiceUri")) {
				voiceUri = jsonObj.optString("voiceUri");
			}
			if (jsonObj.has("imageUrls") && jsonObj.getJSONArray("imageUrls").length() > 0) {
				String[] arr = new String[jsonObj.getJSONArray("imageUrls").length()];
				for (int i = 0; i < arr.length; i++) {
					arr[i] = jsonObj.getJSONArray("imageUrls").getString(i);
				}
				imageUrls = arr;
			}

		} catch (JSONException e) {
			Log.e("JSONException", e.getMessage());
		}

	}

	// 给消息赋值。
	public IssueMessage(Parcel in) {
		String jsonStr = ParcelUtils.readFromParcel(in);// 该类为工具类，消息属性
		try {
			jsonObj = new JSONObject(jsonStr);
			if (jsonObj.has("text")) {
				text = jsonObj.optString("text");
			}
			if (jsonObj.has("area")) {
				area = jsonObj.optString("area");
			}
			if (jsonObj.has("crop")) {
				crop = jsonObj.optString("crop");
			}
			if (jsonObj.has("weather")) {
				weather = jsonObj.optString("weather");
			}
			if (jsonObj.has("manure")) {
				manure = jsonObj.optString("manure");
			}
			if (jsonObj.has("gold")) {
				gold = jsonObj.optString("gold");
			}
			if (jsonObj.has("voiceUri")) {
				voiceUri = jsonObj.optString("voiceUri");
			}
			if (jsonObj.has("imageUrls") && jsonObj.getJSONArray("imageUrls").length() > 0) {
				String[] arr = new String[jsonObj.getJSONArray("imageUrls").length()];
				for (int i = 0; i < arr.length; i++) {
					arr[i] = jsonObj.getJSONArray("imageUrls").getString(i);
				}
				imageUrls = arr;
			}
		} catch (JSONException e) {
			Log.e("JSONException", e.getMessage());
		}
	}

	/**
	 * 读取接口，目的是要从Parcel中构造一个实现了Parcelable的类的实例处理。
	 */
	public static final Creator<IssueMessage> CREATOR = new Creator<IssueMessage>() {

		@Override
		public IssueMessage createFromParcel(Parcel source) {
			return new IssueMessage(source);
		}

		@Override
		public IssueMessage[] newArray(int size) {
			return new IssueMessage[size];
		}
	};

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		ParcelUtils.writeToParcel(dest, jsonObj.toString());
	}

	@Override
	public byte[] encode() {
		jsonObj = new JSONObject();
		try {
			jsonObj.put("text", text);
			jsonObj.put("area", area);
			jsonObj.put("crop", crop);
			jsonObj.put("weather", weather);
			jsonObj.put("manure", manure);
			jsonObj.put("gold", gold);
			jsonObj.put("voiceUri", voiceUri);
			JSONArray imageArr = new JSONArray();
			if (imageUrls != null && imageUrls.length > 0) {
				for (String url : imageUrls) {
					imageArr.put(url);
				}
			}
			jsonObj.put("imageUrls", imageArr);
		} catch (JSONException e) {
			Log.e("JSONException", e.getMessage());
		}

		try {
			return jsonObj.toString().getBytes("UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		return null;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public String getVoiceUri() {
		return voiceUri;
	}

	public void setVoiceUri(String voiceUri) {
		this.voiceUri = voiceUri;
	}

	public String[] getImageUrls() {
		return imageUrls;
	}

	public void setImageUrls(String[] imageUrls) {
		this.imageUrls = imageUrls;
	}

	public String getArea() {
		return area;
	}

	public void setArea(String area) {
		this.area = area;
	}

	public String getCrop() {
		return crop;
	}

	public void setCrop(String crop) {
		this.crop = crop;
	}

	public String getWeather() {
		return weather;
	}

	public void setWeather(String weather) {
		this.weather = weather;
	}

	public String getManure() {
		return manure;
	}

	public void setManure(String manure) {
		this.manure = manure;
	}

	public String getGold() {
		return gold;
	}

	public void setGold(String gold) {
		this.gold = gold;
	}

	public JSONObject getJsonObj() {
		return jsonObj;
	}

	public void setJsonObj(JSONObject jsonObj) {
		this.jsonObj = jsonObj;
	}

}
